<template>
  <div class="content_box">
    <!-- <van-checkbox-group > -->
      <div class="list_box">
        <van-checkbox v-model="item.selected" @change="onChange(item,index)" v-for="(item,index) in list" :key="index" label-position="left" :name="index">
          <div class="box">
            <img class="bj" src="/static/images/coupon_bj.png" alt="">
            <p class="label" v-if="item.couponType == 1">折扣券</p>
            <p class="label" v-if="item.couponType == 0">指定金额</p>
            <p class="label" v-if="item.couponType == 2">满减券</p>
            <div class="nr">
              <div class="left">
                <p class="price" v-if="item.couponType == 1">{{item.discount}}<span>折</span></p>
                <p class="price" v-if="item.couponType == 0"><span>￥</span>{{item.amount}}</p>
                <p class="price" v-if="item.couponType == 2"><span>￥</span>{{item.subtract}}</p>
                <p class="text">{{item.content}}</p>
              </div>
              <p class="line"></p>
              <div class="right">
                <div class="name_box">
                  <div class="name">
                    <p class="title" v-if="item.couponType == 1">折扣券</p>
                    <p class="title" v-if="item.couponType == 0">指定金额</p>
                    <p class="title" v-if="item.couponType == 2">满减券</p>
                    <p class="time">有效期至{{item.endTime}}</p>
                  </div>
                </div>
                <p class="jiesao">{{item.remark}}</p>
              </div>
            </div>
          </div>
        </van-checkbox>
      </div>
    <!-- </van-checkbox-group> -->
    <div class="change_box" >
      <div class="nr" v-if="msg.name">
        <p class="text" v-if="msg.couponType == 1">红包优惠<span>{{msg.discount}}</span>折</p>
        <p class="text" v-if="msg.couponType == 0">红包优惠￥<span>{{msg.amount}}</span></p>
        <p class="text" v-if="msg.couponType == 2">红包优惠￥<span>{{msg.subtract}}</span></p>
        <p class="anniu" @click="submit">确认</p>
      </div>
      <div class="nr" v-else>
        <p class="text">红包优惠￥<span>0</span></p>
        <p class="anniu" @click="cancel">确认</p>
      </div>
    </div>
  </div>
</template>

<script>
import { 
  getUserCouponByOrderPage,
  orderBindCoupon
} from "@/api/index";
import { Debounce } from "@/utils"
export default {
  data(){
    return{
      query:{
        current:1,
        size:10,
        orderId:''
      },
      list:[],
      msg:{},
      radioIndex:[],
    }
  },
  methods:{

    // 提交
    submit:Debounce(function () {
      let data = {
        couponId:this.msg.couponId,
        orderId:this.query.orderId
      }
      orderBindCoupon(data).then(res=>{
        if(res.statusCode == '00000'){
          wx.navigateBack({
            delta: 1
          });
        }else{
          this.$toast(res.message)
        }
      })
    },300),

    // 取消使用优惠券
    cancel:Debounce(function () {
      orderBindCoupon({orderId:this.query.orderId}).then(res=>{
        if(res.statusCode == '00000'){
          wx.navigateBack({
            delta: 1
          });
        }else{
          this.$toast(res.message)
        }
      })
    },300),

    // 选择优惠券
    onChange(item,index){
      if(item.selected){
        this.list[index].selected = false
        this.msg = {}
      }else{
        this.msg = item
        this.list.map(res=>{
          res.selected = false
        })
        this.list[index].selected = true
      }
      console.log(this.msg)
    },

    // 获取列表
    getList(){
      getUserCouponByOrderPage(this.query).then(res=>{
        if(res.statusCode == '00000'){
          let index = res.data.findIndex(item => item.selected)
          if(index != -1){
            this.msg = res.data[index]
          }else{
            res.data[0].selected = true
            this.msg = res.data[0]
          }
          res.data.map(item=>{
            item.startTime = item.startTime.substring(0,10);
            item.endTime = item.endTime.substring(0,10);
          })
          this.list = res.data
        }
      })
    },
  },
  // 下拉刷新
  onPullDownRefresh(){
    wx.showLoading({
        title: '加载中...'
    })
    //在当前页面显示导航条加载动画
    wx.showNavigationBarLoading();
    //显示 loading 提示框。需主动调用 wx.hideLoading 才能关闭提示框
    setTimeout(()=>{
        this.getList()
    },500)
  },
  onShow(){
    this.getList()
  },
  onLoad(options){
    console.log(options)
    this.query.orderId = options.orderId
  }
}
</script>

<style lang="scss" scoped>
.content_box{
  background: #F8F9FA;
  min-height: 100vh;
  .list_box{
    padding-top: 24rpx;
    /deep/ .van-checkbox__label{
      flex: 1;
    }
    /deep/ .van-checkbox__icon--checked{
      background: #80A952;
      border-color:#80A952;
      position: relative;
      z-index: 20;
      right:30rpx;
    }
    /deep/ .van-checkbox__icon{
      position: relative;
      z-index: 20;
      right:30rpx;
    }
    /deep/ .van-checkbox{
      width: 92%;
      margin:0 auto;
      position: relative;
      padding:0;
      margin-bottom:24rpx;
    }
    .box{
      width: 100%;
      height: 208rpx;
      margin:0 auto;
      z-index: 999;
      .bj{
        width: 100%;
        height: 100%;
        position: absolute;
        top:0;
        left:0;
        z-index: 0;
      }
      .bj1{
        width: 100%;
        height: 100%;
        position: absolute;
        top:15rpx;
        left:0;
        z-index: 1;
      }
      .label{
        position: absolute;
        top:0;
        left: 6rpx ;
        padding:0 16rpx;
        height: 40rpx;
        background: linear-gradient(to right,#FF4357, #FE6E34);
        z-index: 20;
        font-size:22rpx;
        color:#fff;
        display: flex;
        align-items: center;
        justify-content: center;
        border-top-left-radius: 16rpx;
        border-bottom-right-radius: 16rpx;
        font-weight: 500;
      }
      .nr{
        position: relative;
        z-index: 30;
        display: flex;
        .left{
          width: 31.4%;
          height: 208rpx;
          display: flex;
          flex-direction:column;
          justify-content: center;
          align-items: center;
          .price{
            display: flex;
            align-items: baseline;
            font-size:70rpx;
            font-weight: bold;
            color:#FF4622;
            margin-top:20rpx;
            span{
              font-size:30rpx;
            }
          }
          .text{
            font-size:24rpx;
            color:#696B6E;
          }
        }  
        .line{
          width: 1rpx;
          height: 160rpx;
          border-right:1px dashed #E4E4E4;
          margin-top:22rpx;
        }
        .right{
          flex: 1;
          padding-left:24rpx;
          .name_box{
            display: flex;
            justify-content: space-between;
            align-items: flex-end;
            margin-top:30rpx;
            margin-bottom:24rpx;
            .name{
              .title{
                color:#242526;
                font-size:32rpx;
                font-weight: 500;
                margin-bottom:12rpx;
              }
              .time{
                color:#666666;
                font-size:24rpx;
              }
            }
            img{
              width: 124rpx;
              height: 124rpx;
              position: absolute;
              right: 6rpx;
              top:8rpx;
            }
          }
          .jiesao{
            color:#999999;
            font-size:22rpx;
          }
        }      
      }
      &.on{
        .label{
          color:#383838;
          background: #F6F6F8 ;
        }
        .price{
          color:#AEAFB0 !important;
        }
        .text{
          color:#B0B1B4 !important ;
        }
        .title{
          color:#8D8E90 !important;
        }
        .time{
          color:#AEAFB0 !important ;
        }
        .jiesao{
          color:#C8C8CA !important;
        }
      }
    }
  }
  .nodata{
    text-align: center;
    color:rgb(119, 118, 118);
    font-size:26rpx;
    margin-top:50rpx
  }
  .change_box{
    position: fixed;
    bottom:80rpx;
    left:0;
    width: 100%;
    .nr{
      width: 90%;
      border-radius: 50rpx;
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin:0 auto;
      .text{
        width: 70%;
        padding-left:40rpx;
        font-size:28rpx;
        font-weight: 500;
        color:#fff;
        height: 90rpx;
        line-height: 90rpx;
        background: #222;
        border-bottom-left-radius: 50rpx;
        border-top-left-radius: 50rpx;
        span{
          font-size:32rpx;
        }
      }
      .anniu{
        width: 30%;
        height: 90rpx;
        line-height: 90rpx;
        text-align: center;
        background: #80A952;
        color:#fff;
        font-size:30rpx;
        border-bottom-right-radius: 50rpx;
        border-top-right-radius: 50rpx;
      }
    }
  }
}
</style>